﻿using System.Xml;
using System.IO;

namespace InnovTech.Util
{
    public static class XmlUtil
    {
        /// <summary>
        /// get the xml document from the given file path
        /// </summary>
        /// <param name="filePath">the path of a file which contains a xml document</param>
        /// <returns>a XmlDocument Instance</returns>
        public static XmlDocument GetDocument(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("the given file: " + filePath + " dosn't exist");
            }
            using (XmlReader reader = XmlReader.Create(filePath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                doc.Normalize();
                return doc;
            }
        }
        /// <summary>
        /// save a XmlDocument instance to the given file path
        /// </summary>
        /// <param name="doc">a XmlDocument instance</param>
        /// <param name="filePath">the path of a file which should have this XmlDocument instance</param>
        public static void SaveDocument(XmlDocument doc, string filePath)
        {
            if (!File.Exists(filePath))
            {
                File.Create(filePath);
            }
            doc.Save(filePath);
        }
    }
}
